<?php

namespace App\Http\Controllers\Fee\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\Fee\FeishuApply\FeeAnnualOrderService;
use App\Service\OpenShop\Wap\SignOrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class PayController extends BaseController
{
    public function detail(Request $request)
    {
        // 传：指定shopId
        // 不传，默认取用户名下所有的
        $shopId = (int)$request->get('shopId');

        $signOrderModel = OpenShopSignOrderModel::getModelByShopId($shopId);

        $data = (new SignOrderService('', $signOrderModel, false, true))
            ->payAnnualDetail(MerchantAppAuth::$userId, [$shopId]);

        return $this->success($data);
    }

    public function getExpired(Request $request)
    {
        $shopId = (int)$request->get('shopId');

        return $this->success(FeeAnnualOrderService::getExpired($shopId));
    }

    /**
     * 年管理费为0的支付情况
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function pay(Request $request): JsonResponse
    {
        $this->validate($request, [
            'orderNo' => 'required|string',
            'bizId'   => 'required|string',
        ]);

        $orderNo = (string)$request->post('orderNo');
        $bizId   = (string)$request->post('bizId');

        FeeAnnualOrderService::pay($orderNo, $bizId);
        return $this->success();
    }
}
